package cn.telling.product.dao.Impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import cn.telling.product.dao.ISupplyAreaDao;
import cn.telling.product.vo.AreaInfoVo;
import cn.telling.utils.AutoInjection;
import cn.telling.utils.BaseUtil;

@Repository("supplyAreaDao")
public class SupplyAreaDaoImpl implements ISupplyAreaDao {
    
    private static final Logger LOGGER = LoggerFactory.getLogger(SupplyAreaDaoImpl.class);
    
    @Autowired
    @Qualifier("film-template")
    private JdbcTemplate jdbcTemplate;

    @Override
    public List<AreaInfoVo> findSupplyAreaBySupplyId(String supplyId) {
        String sql = "select a.id,a.supply_id,a.area_id areaid,a.parent_id parentid,b.areaname,b.areaLevel from supply_area a left join areainfo b on a.area_id=b.areaid where a.supply_id =? and a.del_flag='1'";
        Object args [] = new Object[]{supplyId};
        
        LOGGER.debug("根据供应商ID获取销售区域：" + BaseUtil.logSQL(sql,args));
        return jdbcTemplate.query(sql,args,new RowMapper<AreaInfoVo>() {
            public AreaInfoVo mapRow(ResultSet rs, int rowNum)throws SQLException {
                AreaInfoVo vo = new AreaInfoVo();
                AutoInjection.Rs2Vo(rs, vo, null);
                return vo;
            }
        });
    }

	@Override
	public List<AreaInfoVo> findSupplyAreaByAreaId(String areaId) {
		String sql = "select * from areainfo ai where ai.parentid=?";
    Object args [] = new Object[]{areaId};
    
    LOGGER.debug("根据区域ID获取销售区域：" + BaseUtil.logSQL(sql,args));
    return jdbcTemplate.query(sql,args,new RowMapper<AreaInfoVo>() {
        public AreaInfoVo mapRow(ResultSet rs, int rowNum)throws SQLException {
            AreaInfoVo vo = new AreaInfoVo();
            AutoInjection.Rs2Vo(rs, vo, null);
            return vo;
        }
    });}

}
